package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsComment;
import com.ruoyi.system.domain.dto.UserBaseInfoDTO;
import com.ruoyi.system.domain.ro.BsCommentRO;
import com.ruoyi.system.domain.vo.BsCommentVO;
import com.ruoyi.system.mapper.BsCommentMapper;
import com.ruoyi.system.service.IBsCommentService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 评论记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-25
 */
@RequiredArgsConstructor
@Service
public class BsCommentServiceImpl implements IBsCommentService {

    private final BsCommentMapper baseMapper;
    private final ISysUserService iSysUserService;

    /**
     * 说明 : 统计 被评论总数
     */
    public Integer getTotalCommentByType(String type, String aimId) {
        Integer integer = baseMapper.selectCount(new QueryWrapper<BsComment>().lambda()
                .eq(BsComment::getType, type).eq(BsComment::getAimId, aimId));
        return integer;
    }

    /**
     * 查询评论记录
     */
    @Override
    public BsCommentVO queryById(Long id) {
        BsComment bsComment = baseMapper.selectById(id);
        BsCommentVO bsCommentVO = new BsCommentVO();
        if (bsComment != null) {
            UserBaseInfoDTO userBaseInfoDTO = iSysUserService.selectUserBaseInfo(bsComment.getCreateById(), false);
            BeanUtil.copyProperties(bsComment, bsCommentVO);
            bsCommentVO.setCreateUserInfo(userBaseInfoDTO);
        }
        return bsCommentVO;

    }

    /**
     * 查询评论记录列表
     */
    @Override
    public List<BsCommentVO> queryList(BsCommentRO bo) {
        LambdaQueryWrapper<BsComment> lqw = buildQueryWrapper(bo);
        List<BsComment> list = baseMapper.selectList(lqw);
        Set<Long> userIds = list.stream().map(BsComment::getCreateById).collect(Collectors.toSet());
        Map<Long, UserBaseInfoDTO> userMap = iSysUserService.selectUserBaseInfo(userIds);
        ArrayList<BsCommentVO> result = new ArrayList<>();
        list.forEach(f -> {
            BsCommentVO bsCommentVO = new BsCommentVO();
            BeanUtil.copyProperties(f, bsCommentVO);
            bsCommentVO.setCreateUserInfo(userMap.get(f.getCreateById()));
            result.add(bsCommentVO);
        });
        return result;
    }

    private LambdaQueryWrapper<BsComment> buildQueryWrapper(BsCommentRO bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), BsComment::getType, bo.getType());
        lqw.eq(bo.getCreateById() != null, BsComment::getCreateById, bo.getCreateById());
        lqw.eq(StringUtils.isNotBlank(bo.getAimId()), BsComment::getAimId, bo.getAimId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), BsComment::getContent, bo.getContent());
        lqw.orderByDesc(BsComment::getCreateTime);
        return lqw;
    }

    /**
     * 新增评论记录
     */
    @Override
    public Boolean insertByBo(BsCommentRO bo) {
        BsComment add = BeanUtil.toBean(bo, BsComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改评论记录
     */
    @Override
    public Boolean updateByBo(BsCommentRO bo) {
        BsComment update = BeanUtil.toBean(bo, BsComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsComment entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除评论记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<BsCommentVO> personalCenterCommentList() {
        Long userId = SecurityUtils.getUserId();
        List<BsCommentVO> bsCommentVOS = baseMapper.personalCenterCommentList(userId);
        Set<Long> userId2 = bsCommentVOS.stream().map(BsCommentVO::getCreateById).collect(Collectors.toSet());
        Map<Long, UserBaseInfoDTO> userMap2 = iSysUserService.selectUserBaseInfo(userId2);
        bsCommentVOS.forEach(f -> {
            f.setCreateUserInfo(userMap2.get(f.getCreateById()));
        });
        return bsCommentVOS;
    }
}
